//
//  CustomPicker.m
//  CustomPicker
//
//  Created by Joinwe on 16/3/24.
//  Copyright © 2016年 Joinwe. All rights reserved.
//

#import "CustomPicker.h"
#import "DCChooseBank.h"
#import "DCFlagView.h"
#import "DCFlag.h"


@interface CustomPicker()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, retain) UIView *backView;

@property (nonatomic, retain) NSMutableArray *bigArray;

@property (nonatomic, retain) UIPickerView *bankPiker;

@property (nonatomic, retain) NSArray *imageArray;
@property (nonatomic, retain) NSArray *nameArray;


@property (nonatomic, copy) NSString *imageName;
@property (nonatomic, copy) NSString *bankName;


@property (nonatomic, assign) BOOL isShow;

@end



@implementation CustomPicker

static CustomPicker *_customPicker;

+ (instancetype)sharePickWithImageArray:(NSArray *)imageArray nameArray:(NSArray *)nameArray{

    //    谓词
    static dispatch_once_t predicate;
    
    //    这里的代码只执行一次
    dispatch_once(&predicate,^{
        _customPicker = [[CustomPicker alloc] initPickWithImageArray:imageArray nameArray:nameArray];
    });
    
    
    
    return _customPicker;
}


- (instancetype)initWithImageArray:(NSArray *)imageArray nameArray:(NSArray *)nameArray{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, SCREENSIZE.height, SCREENSIZE.width, 260);
        self.backgroundColor = [UIColor grayColor];
        
        _isShow = NO;
        
        _imageArray = imageArray;
        _nameArray = nameArray;
        
        
        _bigArray = [[NSMutableArray alloc] initWithCapacity:0];
        
        for (int i = 0; i < _nameArray.count; i ++) {
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:0];
            [dic setObject:_nameArray[i] forKey:@"bankName"];
            [dic setObject:_imageArray[i] forKey:@"bankIcon"];
            [_bigArray addObject:dic];
        }
        
        
        _imageName = _imageArray[0];
        _bankName = _nameArray[0];
        
        
        //        添加工具栏
        UIToolbar *topView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, SCREENSIZE.width, 44)];
        [topView setBarStyle:UIBarStyleDefault];
        [self addSubview:topView];
        
        
        UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 44)];
        cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
        cancelButton.adjustsImageWhenHighlighted = NO;
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(cancelBtn) forControlEvents:UIControlEventTouchUpInside];
        [cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        
        UIBarButtonItem * button1 =[[UIBarButtonItem  alloc]initWithCustomView:cancelButton];
        UIBarButtonItem * button2 = [[UIBarButtonItem  alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        
        UIButton *doneBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREENSIZE.width / 3 - 40, 0, 40, 44)];
        doneBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        doneBtn.adjustsImageWhenHighlighted = NO;
        [doneBtn setTitle:@"确定" forState:UIControlStateNormal];
        [doneBtn addTarget:self action:@selector(doneBtn) forControlEvents:UIControlEventTouchUpInside];
        [doneBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithCustomView:doneBtn];
        
        NSArray *buttonArr = [NSArray arrayWithObjects:button1,button2,doneButton, nil];
        [topView setItems:buttonArr];
        
        _bankPiker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, SCREENSIZE.width, 216)];
        _bankPiker.delegate = self;
        _bankPiker.dataSource = self;
        _bankPiker.backgroundColor = [UIColor whiteColor];
        _bankPiker.alpha = 0.5;
        [self addSubview:_bankPiker];
    }
    return self;
}




- (instancetype)initPickWithImageArray:(NSArray *)imageArray nameArray:(NSArray *)nameArray{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, SCREENSIZE.height, SCREENSIZE.width, 260);
        self.backgroundColor = [UIColor grayColor];
        
        
        _imageArray = imageArray;
        _nameArray = nameArray;
        
        
        _bigArray = [[NSMutableArray alloc] initWithCapacity:0];
        
        for (int i = 0; i < _nameArray.count; i ++) {
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:0];
            [dic setObject:_nameArray[i] forKey:@"bankName"];
            [dic setObject:_imageArray[i] forKey:@"bankIcon"];
            [_bigArray addObject:dic];
        }
        
        
        _imageName = _imageArray[0];
        _bankName = _nameArray[0];
        
        
//        添加工具栏
        UIToolbar *topView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, SCREENSIZE.width, 44)];
        [topView setBarStyle:UIBarStyleDefault];
        [self addSubview:topView];
        
        
        UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 44)];
        cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
        cancelButton.adjustsImageWhenHighlighted = NO;
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(cancelBtn) forControlEvents:UIControlEventTouchUpInside];
        [cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        
        UIBarButtonItem * button1 =[[UIBarButtonItem  alloc]initWithCustomView:cancelButton];
        UIBarButtonItem * button2 = [[UIBarButtonItem  alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

        UIButton *doneBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREENSIZE.width / 3 - 40, 0, 40, 44)];
        doneBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        doneBtn.adjustsImageWhenHighlighted = NO;
        [doneBtn setTitle:@"确定" forState:UIControlStateNormal];
        [doneBtn addTarget:self action:@selector(doneBtn) forControlEvents:UIControlEventTouchUpInside];
        [doneBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithCustomView:doneBtn];

        NSArray *buttonArr = [NSArray arrayWithObjects:button1,button2,doneButton, nil];
        [topView setItems:buttonArr];
        
        _bankPiker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, SCREENSIZE.width, 216)];
        _bankPiker.delegate = self;
        _bankPiker.dataSource = self;
        _bankPiker.backgroundColor = [UIColor whiteColor];
        _bankPiker.alpha = 0.5;
        [self addSubview:_bankPiker];
        
        
    }
    return self;
}


#pragma mark Picker数据源和代理方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _bigArray.count;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    DCFlagView *flagView = [DCFlagView flagViewWithPickerView:pickerView];
    flagView.flag = [DCFlag flagWithDictionary:[_bigArray objectAtIndex:row]];
    flagView.backgroundColor = [UIColor clearColor];
    
    return flagView;
}


- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return [DCFlagView flagViewHeight];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    _imageName = _imageArray[row];
    _bankName = _nameArray[row];
}

- (void)doneBtn{
    [self.delegate bankImage:_imageName bankName:_bankName];
    [self removeBankPicker];
}


/**
 *  取消按钮
 */
- (void)cancelBtn{
    [self removeBankPicker];
}

/**
 *  移除Picker
 */
- (void)removeBankPicker{
    
    [UIView animateWithDuration:0.4 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.transform = CGAffineTransformMakeTranslation(0, 260 + 64);
    } completion:^(BOOL finished) {
        // for (UIView * view in self.subviews) {
        //     [view removeFromSuperview];
        // }
        // [self removeFromSuperview];
    }];
    
}


- (void)show{
    [UIView animateWithDuration:0.5 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.transform = CGAffineTransformMakeTranslation(0, - 260);
    } completion:^(BOOL finished) {
        
    }];
}


- (void)dismiss{
    [self removeBankPicker];
}





- (void)dealloc{
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end